import random

from langchain.agents import LLMSingleActionAgent, AgentOutputParser
from langchain.chains.question_answering.map_rerank_prompt import output_parser
from langchain_core.messages.tool import tool_call
from langchain_core.output_parsers import JsonOutputParser, JsonOutputToolsParser, StrOutputParser
from langchain_core.runnables import RunnableSequence, RunnablePassthrough
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI

@tool
def get_temperature(city:str)->int:
    """获取指定城市的当前气温"""
    return random.randint(-20,50)
llm = ChatOpenAI(
    openai_api_key="key",
    openai_api_base="https://api.moonshot.cn/v1",
    model="moonshot-v1-8k",
    temperature=0,
    request_timeout=60,
    max_retries=3,
)


output_parser=JsonOutputToolsParser()
output=StrOutputParser()
# agent_out=AgentOutputParser()
# chain=(RunnablePassthrough()|llm.bind_tools(tools=[get_temperature])|output_parser)
chain=LLMSingleActionAgent(llm=llm,tools=[get_temperature],output_parser=output_parser)
print(chain.invoke('今天靖西天气多少度?'))